var basePath = $("#basePath").val();
$(document).ready(function (){
    menuCss("合同管理");
    queryContractInfo();
    initModal();
});


function queryContractInfo (){
    var contractId = $("#contractId").val();
    var url = basePath + "/contract/queryContractInfo";
    publicAjax(url,"POST",{contractId:contractId},function(data){
        if(data.ret == 200){
            bindContractInfoData(data.data);
        }else{
            flavrShowByTime(data.msg,null,"danger",false);
        }
    });
}

function bindContractInfoData(obj){

    //合同基本信息
    $("#contractNoSpan").html(obj.contractNo == null ? '' : obj.contractNo);
    $("#contractNameSpan").html(obj.contractName == null ? '' : obj.contractName );
    $("#contractTypeNameSpan").html(obj.contractTypeName == null ? '' : obj.contractTypeName);
    $("#hospitalNameSpan").html(obj.hospitalName == null ? '' : obj.hospitalName);
    $("#hospitalContactsSpan").html(obj.firstPartyContacts == null ? '' : obj.firstPartyContacts);
    $("#hospitalPhoneSpan").html(obj.firstPartyPhone == null ? '' : obj.firstPartyPhone);
    $("#supplierNameSpan").html(obj.supplierName == null ? '' : obj.supplierName);
    $("#supplierContactsSpan").html(obj.secondPartyContacts == null ? '' : obj.secondPartyContacts);
    $("#supplierPhoneSpan").html(obj.secondPartyPhone == null ? '' : obj.secondPartyPhone);
    $("#signDateSpan").html(obj.signDate == null ? '' : obj.signDate);
    $("#contractAmountSpan").html(obj.contractAmountStr);
    $("#remarkSpan").html(obj.remark == null ? '' : obj.remark);

    //合同设备信息
    $("#contract_device_list").html("");
    var str = "";
    if(obj.contractDeviceList == null || obj.contractDeviceList.length == 0){
        str = "<tr><td colspan='15' class='text-muted text-center'>暂无设备</td></tr>";
    }else{
       $.each(obj.contractDeviceList,function (index,item) {
           str += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
               "<td class='text-muted text-center'>"+(item.deviceName == null?'':item.deviceName)+"</td>" +
               "<td class='text-muted text-center'>"+(item.deviceModel == null?'':item.deviceModel)+"</td>" +
               "<td class='text-muted text-center'>"+(item.deviceWarrantyPeriod || '')+"</td>" +
               "<td class='text-muted text-center'>"+(item.supplierWarrantyPeriod || '')+"</td>" +
               "<td class='text-muted text-center'>"+(item.manufacturerName == null?'':item.manufacturerName)+"</td>" +
               "<td class='text-muted text-center'>"+(item.maintainerName == null?'':item.maintainerName)+"</td>" +
               "<td class='text-muted text-center'>"+(item.placeOrigin == null?'':item.placeOrigin)+"</td>" +
               "<td class='text-muted text-center'>"+(item.specifications == null?'':item.specifications)+"</td>" +
               "<td class='text-muted text-center'>"+(item.attachment == null?'':item.attachment)+"</td>" +
               "<td class='text-muted text-center'>"+(item.upkeepCycle == null?'':item.upkeepCycle)+"</td>" +
               "<td class='text-muted text-center'>"+(item.priceStr == null?'':item.priceStr)+"</td>" +
               "<td class='text-muted text-center'>"+(item.quantity == null?'':item.quantity)+"</td>" +
               "<td class='text-muted text-center'><a href='javascript:showDeviceActual("+item.contractDeviceId+",\""+item.deviceName+"\")'>"+(item.actualQuantity == null?'':item.actualQuantity)+"</a></td>" +
               "<td class='text-muted text-center'>"+(item.actualAcceptanceQuantity == null?'':item.actualAcceptanceQuantity)+"</td>" +
               "<td class='text-muted text-center'>"+(item.whetherNeedInstall == 1?'是':'否')+"</td>" +
               "<td class='text-muted text-center'>"+(item.remark == null?'':item.remark)+"</td>" +
               "</tr>";
       })

    }
    $("#contract_device_list").html(str);

    //合同文件信息
    $("#contract_file_list").html("");
    var strs = "";
    if(obj.fileList == null || obj.fileList.length == 0){
            strs = "<tr><td colspan='10' class='text-muted text-center'>暂无文件</td></tr>";
    }else{
        $.each(obj.fileList,function (index,item) {
            var fileName = item.fileName + "." + item.fileSuffix;
            strs += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
                "<td class='text-muted text-center'>"+(item.fileTypeName == null?'':item.fileTypeName)+"</td>" +
                "<td class='text-muted text-center'><a href='javascript:showFile(\""+item.fileUrl+"\",\""+fileName+"\")'>"+(item.fileName == null?'':item.fileName)+"</a></td>" +
                "<td class='text-muted text-center'>"+(item.startDate == null?'':item.startDate)+"</td>" +
                "<td class='text-muted text-center'>"+(item.endDate == null?'':item.endDate)+"</td>" +
                "<td class='text-muted text-center'>"+(item.fileSize == null?'':item.fileSize)+"</td>" +
                "<td class='text-muted text-center'><button type='button' class='btn btn-danger btn-xs' title='下载' onclick='fileDownload(\""+item.fileName+"\",\""+item.fileUrl+"\",\""+item.fileSuffix+"\")'><i class='fa fa-fw fa-download'></i></button></td>" +
                "</tr>";
        });
    }
    $("#contract_file_list").html(strs);


    //合同发货总览 estimate_actual
    $("#estimate_list").html("");
    var strss = "";
    if(obj.estimateActualList == null || obj.estimateActualList.length == 0){
        strss = "<tr><td colspan='10' class='text-muted text-center'>暂无发货信息</td></tr>";
    }else{
        $.each(obj.estimateActualList,function (index,item) {
            strss += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
                "<td class='text-muted text-center'>"+(item.estimateSendDate == null?'':item.estimateSendDate)+"</td>" +
                "<td class='text-muted text-center'><a href='javascript:showQuantity(\""+item.contractEstimateSendId+"\")'>"+(item.estimateQuantity == null?'':item.estimateQuantity)+"</a></td>" +
                "<td class='text-muted text-center'>"+(item.userName == null?'':item.userName)+"</td>" +
                "<td class='text-muted text-center'><a href='javascript:showQuantity(\""+item.contractEstimateSendId+"\")'>"+(item.actualQuantity == null?'':item.actualQuantity)+"</a></td>" +
                "<td class='text-muted text-center'>"+(item.remark == null?'':item.remark)+"</td>" +
                "</tr>";
        });
    }
    $("#estimate_list").html(strss);
}

function showDeviceActual(contractDeviceId,deviceName){
        var url = basePath + "/contract/deviceEstimateActual"
        publicAjax(url,"POST",{contractDeviceId:contractDeviceId},function (data) {
           if(data.ret == 200){
               bindEstimateActualDevice(data.data,deviceName);
           }else{
               flavrShowByTime(data.msg,null,"danger",false);
           }
        })
}

function bindEstimateActualDevice(obj,deviceName){
    var name = deviceName  + "的发货信息";
    $("#estimate_actual_list_model").iziModal("setTitle",name);

    $("#estimate_actual_list").html("");
    var str = "";
    if(obj == null || obj.length == 0){
        str = "<tr><td colspan='10' class='text-muted text-center'>暂无发货信息</td></tr>";
    }else{
        $.each(obj,function (index,item) {
            str += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
                "<td class='text-muted text-center'>"+(item.estimateSendDate == null?'':item.estimateSendDate)+"</td>" +
                "<td class='text-muted text-center'>"+(item.userName == null?'':item.userName)+"</td>" +
                "<td class='text-muted text-center'>"+(item.estimateQuantity == null?'':item.estimateQuantity)+"</td>" +
                "<td class='text-muted text-center'>"+(item.actualQuantity == null?'':item.actualQuantity)+"</td>" +
                "<td class='text-muted text-center'>"+(item.remark == null?'':item.remark)+"</td>" +
                "</tr>";
        })
    }
    $("#estimate_actual_list").html(str);
    $("#estimate_actual_list_model").iziModal("open",this);
}


function fileDownload(fileName,filePath,fileSuffix){
    location.href= basePath+"/file/fileDownload?filePath="+filePath+"&fileName="+fileName;
}

function showFile(url,file){
    $("#file_show_modal").iziModal({
        zindex:999999,
        headerColor: '#00c0ef',
        title: '预览',
        subtitle: file,
        overlayClose: true,
        history:false,
        iframe : true,
        iframeURL:url,
        fullscreen: true,
        openFullscreen: false,
        onClosed:function(){
            $('#file_show_modal').iziModal('destroy');
        }
    });
    $("#file_show_modal").iziModal('open');
}

function initModal(){
    $("#expected_delivery_assets").iziModal({
        title: "1",
        headerColor: '#00c0ef',
        rtl: false,
        width: 800,
        zindex: 1042,
        focusInput: false,
        group: '',
        loop: false,
        history: false,
        overlayClose: false,
        autoOpen: false, // Boolean, Number
        bodyOverflow: true,
        onOpening: function () {},
        onOpened: function () {},
        onClosing: function () {},
        onClosed: function () {}
    });

    $("#estimate_actual_list_model").iziModal({
        title: "1",
        headerColor: '#00c0ef',
        rtl: false,
        width: 800,
        zindex: 1042,
        focusInput: false,
        group: '',
        loop: false,
        history: false,
        overlayClose: false,
        autoOpen: false, // Boolean, Number
        bodyOverflow: true,
        onOpening: function () {},
        onOpened: function () {},
        onClosing: function () {},
        onClosed: function () {}
    });

}


function showQuantity(estimateId){

    var  url=basePath + "/contract/estimateQuantity";
    $("#expected_delivery_assets").iziModal("setTitle","发货设备信息");
    publicAjax(url,"POST",{estimateId:estimateId},function (data) {
        if(data.ret == 200){
            bindEstimateDeviceData(data.data);
        }else{
            flavrShowByTime(data.msg,null,"danger",false);
        }
    });
}

function bindEstimateDeviceData(obj){
    $("#estimate_device_list").html("");
    var str = "";
    $.each(obj,function (index,item) {
        str += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
            "<td class='text-muted text-center'>"+(item.deviceName == null?'':item.deviceName)+"</td>" +
            "<td class='text-muted text-center'>"+(item.deviceModel == null?'':item.deviceModel)+"</td>" +
            "<td class='text-muted text-center'>"+(item.placeOrigin == null?'':item.placeOrigin)+"</td>" +
            "<td class='text-muted text-center'>"+(item.specifications == null?'':item.specifications)+"</td>" +
            "<td class='text-muted text-center'>"+(item.upkeepCycle == null?'':item.upkeepCycle)+"</td>" +
            "<td class='text-muted text-center'>"+(item.whetherNeedInstall == 1?'是':'否')+"</td>" +
            "<td class='text-muted text-center'>"+(item.estimateQuantity == null?'':item.estimateQuantity)+"</td>" +
            "<td class='text-muted text-center'>"+(item.actualQuantity == null?'':item.actualQuantity)+"</td>" +
            "</tr>";
    });
    $("#estimate_device_list").html(str);
    $("#expected_delivery_assets").iziModal("open",this);
}

